package org.nox.helloseries.rest;

import javax.inject.Named;

import org.nox.helloseries.domain.RetourLogin;
import org.nox.helloseries.domain.User;

import restx.annotations.GET;
import restx.annotations.RestxResource;
import restx.factory.Component;
import restx.jongo.JongoCollection;
import restx.security.PermitAll;

@Component
@RestxResource
@PermitAll
public class HelloSeriesLoginResource
{
    private final JongoCollection users;

    public HelloSeriesLoginResource( @Named( "users" ) JongoCollection users )
    {
        this.users = users;
    }

    @GET( "/login" )
    public RetourLogin login( String username, String password )
    {
        RetourLogin retour = new RetourLogin();

        User user = users.get().findOne( "{username: #, password : #}", username, password ).as( User.class );

        if ( user != null )
        {
            retour.setCode( 0 );
            retour.setUser( user );
        }
        else
        {
            retour.setCode( 1 );
        }
        return retour;
    }
}